package com.example.yunhailutong.zhongtan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yunhailutong.zhongtan.R;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class MessageDetailsActivity extends AppCompatActivity {
    private TextView title;
    private ImageView back;
    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_details);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        title = (TextView) findViewById(R.id.title);
        webView= (WebView) findViewById(R.id.webview);
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title.setText(bundle.getString("title"));
        try {
            String desc = URLDecoder.decode(bundle.getString("content"), "UTF-8");
            loadHtmlData(desc);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public void loadHtmlData(String data) {
        webView.setWebViewClient(new WebViewClient());
        String html=getHtmlData(data);
        //使用简单的loadData()方法总会导致乱码，有可能是Android API的Bug
        //webView.loadData(data, "text/html", "GBK");
        webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
    }
    private String getHtmlData(String bodyHTML) {
        String head = "<head>"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:auto; height:auto;}</style>"
                + "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }
}
